package com.binaryTree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class BinaryTreeInorderTraversal0205_94 {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        inorder(root,res);
        return res;

    }

    private void inorder(TreeNode root, List<Integer> res) {
        if(root == null) return;

        inorder(root.left,res);
        res.add(root.val);
        inorder(root.right,res);
    }
}

class BinaryTreeInorderTraversal{
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while (cur != null || !stack.isEmpty()){
            if(cur !=null){
                stack.push(cur);
                cur = cur.left;
            }else {
                cur = stack.pop();
                res.add(cur.val);
                cur = cur.right;
            }
        }
        return res;
    }
}

class Inorder{
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> sta = new Stack<>();
        if(root != null) sta.push(root);

        while (!sta.isEmpty()){
            TreeNode node = sta.peek();
            if(node != null){
                sta.pop();
                if(node.right != null) sta.push(node.right);
                sta.push(node);
                sta.push(null);
                if(node.left != null) sta.push(node.left);
            }else {
                sta.pop();
                node = sta.pop();
                res.add(node.val);
            }
        }
        return res;
    }
}


